<template>
	<view>
		<!-- 设置 -->
		<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;" @click="clearCache">
			<view class="gui-list-body gui-border-b">
				<view class="gui-list-title">
					<text class="gui-list-title-text gui-primary-text">清理缓存</text>
					<text class="gui-color-gray">{{cacheSize}}</text>
				</view>
			</view>
			<text class="gui-list-arrow-right gui-icons gui-color-gray-light">&#xe601;</text>
		</view>
		<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;" @click="un_wxlogin">
			<view class="gui-list-body gui-border-b">
				<view class="gui-list-title">
					<text class="gui-list-title-text gui-primary-text">解除微信登录</text>
				</view>
			</view>
			<text class="gui-list-arrow-right gui-icons gui-color-gray-light">&#xe601;</text>
		</view>
		<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;">
			<view class="gui-list-body gui-border-b">
				<view class="gui-list-title gui-color-blue" @click="downloadApp">
					<text class="gui-list-title-text gui-primary-text">下载APP</text>
				</view>
			</view>
			<view @click="share_app" class="gui-color-green">
				分享到微信
			</view>
		</view>
		<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;"
			v-for="(item,index) in menus" @click="menu_click(item)">
			<!-- <navigator open-type="navigate" class="gui-list-items" v-for="(item,index) in menus" :url="item.url"> -->

			<view class="gui-list-body gui-border-b">
				<view class="gui-list-title">
					<text class="gui-list-title-text gui-primary-text">{{item.text}}</text>
				</view>
			</view>
			<text class="gui-list-arrow-right gui-icons gui-color-gray-light">&#xe601;</text>
			<!-- </navigator> -->
		</view>
		<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;" @click="logout">
			<view class="gui-list-body gui-border-b">
				<view class="gui-list-title">
					<text class="gui-list-title-text gui-primary-text">退出登录</text>
				</view>
			</view>
		</view>
		<view class="gui-text-center gui-margin-top gui-color-gray" @click="is_log_show">
			Ver：V{{version}}
		</view>
		<view v-if="log_show">
			
			<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;">
				<view class="gui-list-body gui-border-b" @click="to_school()">
					<view class="gui-list-title">
						<text class="gui-list-title-text gui-primary-text">新工匠学校</text>
					</view>
				</view>
				
			</view>
			
			<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;">
				<view class="gui-list-body gui-border-b">
					<view class="gui-list-title">
						<text class="gui-list-title-text gui-primary-text">是否更新预览版本{{is_Preview}}</text>
					</view>
				</view>
				<switch :checked="is_Preview" @change="change_Preview" />
			</view>
			<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;">
				<view @click="test_user" class="gui-list-body gui-border-b">
					<view class="gui-list-title">
						<text class="gui-list-title-text gui-primary-text">测试用户</text>
					</view>
				</view>
			</view>

			<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;">
				<navigator url="/pages/pay/pay_test/pay_test" class="gui-list-body gui-border-b">
					<view class="gui-list-title">
						<text class="gui-list-title-text gui-primary-text">支付测试</text>
					</view>
				</navigator>
			</view>
			
		
			<view class="gui-dark-bg-level-3 gui-list-items" style="width: 95%; margin: 0 auto;">
				<navigator url="/pages/test/h5/h5" class="gui-list-body gui-border-b">
					<view class="gui-list-title">
						<text class="gui-list-title-text gui-primary-text">H5交互测试(本地)</text>
					</view>
				</navigator>
			</view>
			<view>
				首页版本:{{home_ver.ver}}
			</view>
		</view>

		<!-- <view v-show="log_show" class="gui-margin-top gui-padding gui-border gui-border-radius gui-bg-gray">
			{{log}}
		</view> -->
	</view>
</template>

<script>
	var sweixin;
	import CryptoJS from '@/Grace6/js/crypto-js.js'
	import silenceUpdate from '@/uni_modules/rt-uni-update/js_sdk/silence-update.js'
	export default {
		data() {
			return {
				sweixin: '',
				user: uni.getStorageSync("member"),
				page_url: '/pages/web/page?name=测试页面',
				version: '',
				cacheSize: 0,
				is_Preview: null,
				log_show: false,
				log: uni.getStorageSync("log"),
				menus: [{
						id: 0,
						text: "隐私政策",
						url: '/pages/login/agreement/agreement?type=privacy'
					}, {
						id: 1,
						text: "修改密码",
						url: 'chage_password'
					},
					{
						id: 2,
						text: "检查更新",
						url: '/pages/home/index'
					},
					{
						id: 3,
						text: "注销账号",
						url: ''
					},

				],
				system: null,
				touchNum: null,
				Bearer : uni.getStorageSync('Bearer'),
				home_ver:0
			}
		},
		onLoad() {
			// #ifdef APP-PLUS
			let self = this
			this.get_CacheSize();
			this.WeChat();
			if (uni.getSystemInfoSync().platform === 'android' || uni.getSystemInfoSync().platform === 'ios') {
				// console.log('当前为App环境');
				plus.runtime.getProperty(plus.runtime.appid, (appInfo) => {
					self.version = appInfo.version
				});
			} else {
				console.log('当前非App环境');
			}
			// #endif

			this.is_Preview = uni.getStorageSync("is_PreviewVer") || false;
			console.log("is_Preview", this.is_Preview)

			let info = uni.getSystemInfoSync();
			this.system = info.platform;
			let page_home=uni.getStorageSync("page_首页");
			console.log("page_home",page_home);
			this.home_ver=page_home.ver;
		},
		methods: {
			WeChat() {
				// #ifdef APP-PLUS
				plus.share.getServices(function(s) {
					var shares = {};
					for (var i = 0; i < s.length; i++) {
						var t = s[i];
						console.log('WeChat-t: ', t);
						shares[t.id] = t;
						console.log('WeChat-t.id: ', t.id);
					}
					sweixin = shares['weixin'];
				}, function(e) {
					console.log("获取分享服务列表失败：" + e.message);
				});
				// #endif
			},
			logout() {
				this.$comm.logout();
			},
			
			to_school(){
				let url="https://lg.boyaltd.cn/H5/school";
				uni.navigateTo({
					url:"/pages/web/web?url=" + url + "&Bearer=" + this.Bearer + '&showTitle=false'
				});
				
			},
			h5_card() {
				//this.$comm.nav_to("https://app.lgwisdomunion/app.html#/login&full=1","卡包","nav",true);
				this.$comm.nav_to("http://localhost:8081/#/pages/card/card_index", "卡包", "nav", true);
				// this.$comm.nav_to("https://jf.lgwisdomunion.cn/h5_card/#/pages/card/card_index?uuid={uuid}", "卡包", "nav",
				// 	true, true);
			},
			h5_wugong() {
				this.$comm.nav_to("https://jf.lgwisdomunion.cn/h5_card/#/pages/card/wugong/indexZqGq?uuid={uuid}", "物供选择",
					"nav", true, true);
			},
			wugong_test() {
				//this.$comm.nav_to("http://localhost:8081/#/pages/card/wugong/indexZqGq?uuid={uuid}&full=1","物供测试","nav",true);
				this.$comm.nav_to("https://h5.lgwisdomunion.cn/#/pages/card/wugong/indexZqGq?uuid={uuid}", "物供测试", "nav",
					true, true);
			},
			change_Preview(d) {
				//是否更新预览版本
				console.log('是否更新预览版本：', d);
				this.is_Preview = d.detail.value;
				uni.setStorageSync("is_PreviewVer", this.is_Preview.toString());
			},
			un_wxlogin() {
				let self = this;
				uni.showModal({
					title: "解除绑定",
					content: "您确定解除微信登录绑定吗",
					success(e) {
						if (e.confirm) {
							self.$boya.NPost("/app/un_wxbind", null, function(res) {
								console.log(res);
								uni.showToast({
									title: res.msg
								})
							})
						}
					}
				})
			},
			// 获取app缓存大小
			get_CacheSize() {
				// #ifdef APP-PLUS
				var self = this;
				plus.cache.calculate(function(size) { //size是多少个字节单位是b
					//做下面相应的处理
					if (size < 1024) {
						self.cacheSize = size + 'B';
					} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
						self.cacheSize = Math.floor(size / 1024 * 100) / 100 + 'KB';
					} else if (size / 1024 / 1024 >= 1) {
						self.cacheSize = Math.floor(size / 1024 / 1024 * 100) / 100 + 'M';
					}
				});
				// #endif
			},
			// 清除缓存
			clearCache() {
				//uni.clearStorage();
				// #ifdef APP-PLUS
				var self = this;
				//使用plus.cache.clear 清除应用中的缓存数据
				plus.cache.clear(function() {
					uni.showToast({
						title: '清除成功!',
						icon: 'none',
						success() {
							self.cacheSize = '0 B'
						}
					})
				});
				// #endif
				// uni.reLaunch({
				// 	url:"../login/login"
				// });
			},
			menu_click(item) {
				let self = this
				if (item.id == 3) {
					uni.showModal({
						title: '提示',
						content: '您确定注销该账号吗？',
						success: function(res) {
							if (res.confirm) {
								self.$boya.msg('您的账号即将审核注销')
							} else if (res.cancel) {
								self.$boya.msg('您已取消')
							}
						}
					});

				} else if (item.id != 2) {
					uni.navigateTo({
						url: item.url
					})
				} else {
					this.check_update();
				}
			},
			is_log_show() {
				let self = this
				self.touchNum++
				console.log(this.touchNum);
				
				if (self.touchNum >= 15) {
					self.log_show = !this.log_show
					self.touchNum = 0
				}
					
				

			},
			// 检查更新
			check_update() {
				// #ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (inf) => {

					let data = {
						edition_type: plus.runtime.appid,
						version_type: uni.getSystemInfoSync().platform, //android或者ios
						edition_number: inf.versionCode, // 打包时manifest设置的版本号 
						is_previewver: uni.getStorageSync("is_PreviewVer") // 不发行的版本更新
					}
					console.log('检查更新', data);
					this.$boya.NPost("/app/getVer", data, function(res) {
						if (!res.data.hasOwnProperty("edition_number")) return;
						//res.data.xxx根据后台返回的数据决定（我这里后端返回的是data），所以是res.data.data
						//判断后台返回版本号是否大于当前应用版本号 && 是否发行 （上架应用市场时一定不能弹出更新提示）
						let is_PreviewVer = uni.getStorageSync("is_PreviewVer");
						let edition_issue = res.data.edition_issue == 1;
						console.log("is_PreviewVer", is_PreviewVer);
						if (is_PreviewVer) {
							edition_issue = true;
						}
						console.log(res.data.edition_number, Number(inf.versionCode))
						console.log(edition_issue);
						if (Number(res.data.edition_number) > Number(inf.versionCode) && edition_issue) {
							//如果是wgt升级，并且是静默更新 （注意！！！ 如果是手动检查新版本，就不用判断静默更新，请直接跳转更新页，不然点击检查新版本后会没反应）
							if (res.data.package_type == 1 && res.data.edition_silence == 1) {
								//调用静默更新方法 传入下载地址
								console.log("调用静默更新方法", res.data.edition_url);
								uni.showToast({
									title: "发现新版本,开始后台更新!",
									icon: "none"
								})
								let is_restart = true;
								// if(res.data.edition_force==0)
								// {
								// 	is_restart=false;
								// }
								silenceUpdate(res.data.edition_url, is_restart)
							} else {
								console.log("跳转更新页面");
								//跳转更新页面 （注意！！！如果pages.json第一页的代码里有一打开就跳转其他页面的操作，下面这行代码最好写在setTimeout里面设置延时3到5秒再执行）
								setTimeout(function() {
									// 这里是要延迟执行的代码
									uni.navigateTo({
										url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
											JSON.stringify(res.data)
									});
								}, 4000); // 5000毫秒等于5秒
							}
						} else {

							// 如果是手动检查新版本 需开启以下注释
							uni.showModal({
								title: '提示',
								content: '已是最新版本',
								showCancel: false
							})
						}
					}, null, false);
				});
				// #endif
			},

			downloadApp() {
				plus.runtime.openURL("https://new.lgwisdomunion.cn/api/app/down")
			},
			share_app() {
				uni.share({
					provider: "weixin",
					scene: "WXSceneSession",
					type: 0,
					href: "https://new.lgwisdomunion.cn/api/app/down",
					title: "柳钢之家",
					summary: "我正在使用【柳网之家】，赶紧跟我一起来体验！在浏览器打开下载",
					imageUrl: "https://new.lgwisdomunion.cn/logo.png",
					success: function(res) {
						console.log("success:" + JSON.stringify(res));
						uni.showToast({
							title: "分享成功"
						})
					},
					fail: function(err) {
						console.log("fail:" + JSON.stringify(err));

					}
				});
			},
			to_page() {
				uni.navigateTo({
					url: this.page_url
				})
			},
			encrypt(word) {
				let encrypted = CryptoJS.AES.encrypt(word, keyStr, {
					iv: ivStr,
					mode: CryptoJS.mode.CBC,
					padding: CryptoJS.pad.Pkcs7
				});
				return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
			},
			to_mini() {

				let keyStr = CryptoJS.enc.Utf8.parse('96df30d7f54d41c0f3c6eb60caf2eea9');
				let ivStr = CryptoJS.enc.Utf8.parse('b1aeb14d7e22c4c5');

				function encrypt(word) {
					let encrypted = CryptoJS.AES.encrypt(word, keyStr, {
						iv: ivStr,
						mode: CryptoJS.mode.CBC,
						padding: CryptoJS.pad.Pkcs7
					});
					return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
				}

				let plainText = this.user.uuid + '|' + this.user.chinaname;
				let encryption = encrypt(plainText);
				let encryptedText = btoa(encryption)
				console.log('加密1: ', encryption);
				console.log('加密2: ', encryptedText);

				this.$comm.nav_to('https://cook.boyaltd.cn/h5/#/?token=' + encryptedText, "柳钢康养服务")
				// window.location.href = 'https://cook.boyaltd.cn/h5/#/?token=' + encryptedText

				// let appid = "gh_2d2f81d8b114";
				// let path = "/pages/index/index?code=" + encryptedText
				// // #ifdef H5
				// console.log("H5H5H5H5H5H5H5H5");
				// uni.webView.postMessage({
				// 	data: {
				// 		type: "open_wexin",
				// 		data: {
				// 			app_id: appid,
				// 			path: path
				// 		}
				// 	},
				// })
				// // #endif
				// // #ifdef APP-PLUS
				// console.log("APP-PLUSAPP-PLUSAPP-PLUS");
				// sweixin ? sweixin.launchMiniProgram({
				// 	id: appid,
				// 	path: path
				// }) : plus.nativeUI.alert('当前环境不支持微信操作!');
				// // #endif
			},
			test_api() {
				this.$boya.NPost("/app/auth/user").then(res => {
					console.log(res);
				}, null, err => {
					console.log(err);
				})
			},
			test_webh5(url) {
				this.$comm.nav_to(url, "问答")
			},
			card_index() {
				// let url = "url=https://h5.lgwisdomunion.cn/#/pages/card/card_index&token=true&title=卡包";
				// uni.navigateTo({
				// 	url: "/pages/web/web?" + url,

				// })
				uni.navigateTo({
					url: "/pages/card/card_index"
				})
			},
			test_user(){
				console.log("test_user");
				uni.navigateTo({
					url:"/pages/test/change_user",
					success() {
						
					},
					fail(err) {
						console.log(err);
					}
				})
			}
		},

	}
</script>

<style>

</style>